package com.example.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Spring获取bean工具类
 * @author ChenXiaoBin
 * on 2020-03-03
 */
@Component
public class SpringWrap implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    /**
     * getBean
     *
     * @param beanName
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        isInjected();
        return (T) applicationContext.getBean(beanName);
    }

    /**
     * getBean
     *
     * @param requiredType
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> requiredType) {
        isInjected();
        return applicationContext.getBean(requiredType);
    }

    /**
     * destroy
     *
     * @throws Exception
     */
    public void destroy() throws Exception {
        SpringWrap.applicationContext = null;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        SpringWrap.applicationContext = applicationContext;
    }

    /**
     * 判断是否注入
     *
     * @return
     */
    public static void isInjected() {
        if (SpringWrap.applicationContext == null) {
            throw new RuntimeException("springUtils APPLICATION_CONTENT is not injected!");
        }
    }

    public static ApplicationContext getApplicationContent() {
        return applicationContext;
    }

    /**
     * getBean
     *
     * @param requiredType
     * @param <T>
     * @return
     */
    public static <T> Map<String, T> getBeanByMap(Class<T> requiredType) {
        isInjected();
        return applicationContext.getBeansOfType(requiredType);
    }


    /**
     * 添加bean 单例模式
     *
     * @param beanName
     * @param object
     * @return true添加成功  false  bean已存在
     */
    public static Boolean addBean(String beanName, Object object) {
        isInjected();
        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) SpringWrap.getApplicationContent();
        DefaultListableBeanFactory dbf = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();
        if (dbf.containsBeanDefinition(beanName)) {
            return false;
        }
        dbf.registerSingleton(beanName, object);
        return true;
    }

    /**
     * 添加bean
     *
     * @param beanName
     * @param definition
     * @return true添加成功  false  bean已存在
     */
    public static Boolean addBean(String beanName, GenericBeanDefinition definition) {
        isInjected();
        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) SpringWrap.getApplicationContent();
        DefaultListableBeanFactory dbf = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();
        if (dbf.containsBeanDefinition(beanName)) {
            return false;
        }
        dbf.registerBeanDefinition(beanName, definition);
        return true;
    }

    /**
     * 移除bean
     *
     * @param beanName
     * @return true移除成功  false  bean不存在
     */
    public static Boolean removeBean(String beanName) {
        isInjected();
        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) SpringWrap.getApplicationContent();
        DefaultListableBeanFactory dbf = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();
        if (!dbf.containsBeanDefinition(beanName)) {
            return false;
        }
        dbf.removeBeanDefinition(beanName);
        return true;
    }
}
